#Boa:Frame:JanelaPrincipal

import wx
import Frame_CadastroUsuario
import Frame_PaginaUsuario
import Frame_TelaApresentacao

from Class_GerenciadorUsuario import *


def create(parent):
    return JanelaPrincipal(parent)

[wxID_JANELAPRINCIPAL, wxID_JANELAPRINCIPALBUTTON1, 
 wxID_JANELAPRINCIPALCADASTRAR, wxID_JANELAPRINCIPALLOGIN, 
 wxID_JANELAPRINCIPALPANEL1, wxID_JANELAPRINCIPALSTATICLINE1, 
 wxID_JANELAPRINCIPALSTATICLINE2, wxID_JANELAPRINCIPALSTATICTEXT1, 
 wxID_JANELAPRINCIPALSTATICTEXT2, wxID_JANELAPRINCIPALSTATICTEXT3, 
 wxID_JANELAPRINCIPALSTATICTEXT4, wxID_JANELAPRINCIPALSTATICTEXT5, 
 wxID_JANELAPRINCIPALTEXTCTRL1, wxID_JANELAPRINCIPALTEXTCTRL2, 
 wxID_JANELAPRINCIPALTEXTCTRL3, wxID_JANELAPRINCIPALWINDOW1, 
] = [wx.NewId() for _init_ctrls in range(16)]

class JanelaPrincipal(wx.Frame):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Frame.__init__(self, id=wxID_JANELAPRINCIPAL,
              name=u'JanelaPrincipal', parent=prnt, pos=wx.Point(658, 182),
              size=wx.Size(421, 483), style=wx.DEFAULT_FRAME_STYLE,
              title='Frame1')
        self.SetClientSize(wx.Size(405, 445))
        self.Center(wx.BOTH)

        self.window1 = wx.Window(id=wxID_JANELAPRINCIPALWINDOW1, name='window1',
              parent=self, pos=wx.Point(0, 0), size=wx.Size(405, 445), style=0)

        self.panel1 = wx.Panel(id=wxID_JANELAPRINCIPALPANEL1, name='panel1',
              parent=self.window1, pos=wx.Point(-85, 39), size=wx.Size(575,
              270), style=wx.TAB_TRAVERSAL)
        self.panel1.Center(wx.HORIZONTAL)

        self.Cadastrar = wx.Button(id=wxID_JANELAPRINCIPALCADASTRAR,
              label=u'Cadastre-se J\xe1', name=u'Cadastrar', parent=self.panel1,
              pos=wx.Point(209, 40), size=wx.Size(157, 32), style=0)
        self.Cadastrar.Center(wx.HORIZONTAL)
        self.Cadastrar.Bind(wx.EVT_BUTTON, self.OnCadastrarButton,
              id=wxID_JANELAPRINCIPALCADASTRAR)

        self.staticText1 = wx.StaticText(id=wxID_JANELAPRINCIPALSTATICTEXT1,
              label=u'Cadastre-se j\xe1 no Gerenciador de Festas!',
              name='staticText1', parent=self.panel1, pos=wx.Point(187, 8),
              size=wx.Size(201, 13), style=0)
        self.staticText1.Center(wx.HORIZONTAL)

        self.staticText2 = wx.StaticText(id=wxID_JANELAPRINCIPALSTATICTEXT2,
              label=u'Se j\xe1 \xe9 cadastrado ent\xe3o fa\xe7a seu logn:',
              name='staticText2', parent=self.panel1, pos=wx.Point(191, 109),
              size=wx.Size(192, 13), style=0)
        self.staticText2.Center(wx.HORIZONTAL)

        self.textCtrl1 = wx.TextCtrl(id=wxID_JANELAPRINCIPALTEXTCTRL1,
              name='textCtrl1', parent=self.panel1, pos=wx.Point(272, 144),
              size=wx.Size(100, 21), style=0, value=u'admin')

        self.textCtrl2 = wx.TextCtrl(id=wxID_JANELAPRINCIPALTEXTCTRL2,
              name='textCtrl2', parent=self.panel1, pos=wx.Point(272, 184),
              size=wx.Size(100, 21), style=wx.TE_PASSWORD, value=u'123')

        self.staticLine1 = wx.StaticLine(id=wxID_JANELAPRINCIPALSTATICLINE1,
              name='staticLine1', parent=self.panel1, pos=wx.Point(123, 88),
              size=wx.Size(328, 2), style=0)
        self.staticLine1.Center(wx.HORIZONTAL)

        self.Login = wx.Button(id=wxID_JANELAPRINCIPALLOGIN, label=u'Login',
              name=u'Login', parent=self.panel1, pos=wx.Point(247, 231),
              size=wx.Size(80, 24), style=0)
        self.Login.Center(wx.HORIZONTAL)
        self.Login.Bind(wx.EVT_BUTTON, self.OnLoginButton,
              id=wxID_JANELAPRINCIPALLOGIN)

        self.staticText3 = wx.StaticText(id=wxID_JANELAPRINCIPALSTATICTEXT3,
              label=u'Login', name='staticText3', parent=self.panel1,
              pos=wx.Point(224, 144), size=wx.Size(25, 13), style=0)

        self.staticText4 = wx.StaticText(id=wxID_JANELAPRINCIPALSTATICTEXT4,
              label=u'Senha', name='staticText4', parent=self.panel1,
              pos=wx.Point(224, 184), size=wx.Size(30, 13), style=0)

        self.staticLine2 = wx.StaticLine(id=wxID_JANELAPRINCIPALSTATICLINE2,
              name='staticLine2', parent=self.window1, pos=wx.Point(40, 312),
              size=wx.Size(328, 2), style=0)

        self.staticText5 = wx.StaticText(id=wxID_JANELAPRINCIPALSTATICTEXT5,
              label=u'Ou entre com a URL do Evento:', name='staticText5',
              parent=self.window1, pos=wx.Point(128, 328), size=wx.Size(153,
              13), style=0)

        self.textCtrl3 = wx.TextCtrl(id=wxID_JANELAPRINCIPALTEXTCTRL3,
              name='textCtrl3', parent=self.window1, pos=wx.Point(48, 376),
              size=wx.Size(256, 24), style=0, value=u'http://')

        self.button1 = wx.Button(id=wxID_JANELAPRINCIPALBUTTON1, label=u'Ir',
              name='button1', parent=self.window1, pos=wx.Point(312, 376),
              size=wx.Size(40, 23), style=0)
        self.button1.Bind(wx.EVT_BUTTON, self.OnButton1Button,
              id=wxID_JANELAPRINCIPALBUTTON1)

    def __init__(self, parent):
        self._init_ctrls(parent)
        self.G = GerenciadorUsuario()
        self.G.Conexao.CriarDB()


    def OnLoginButton(self, event):
        
        Login = self.textCtrl1.GetValue()
        Senha = self.textCtrl2.GetValue()
        
        Dados = self.G.PesquisarAcesso("Login",Login)
        if len(Dados) == 0:
            #Exibir Mensagem de Erro
            m = "Login e/ou Senha invalidos!"
            t = "Cadastro de Usuario"
            wx.MessageBox(m,t,wx.ICON_ERROR)
            #Limpa os campos de Login e Senha
            self.textCtrl1.SetValue('')
            self.textCtrl2.SetValue('')
            self.textCtrl1.SetFocus()
            
        print Dados
            
        Log = Dados[0][1]
        Sen = Dados[0][2]
        
        #Verifica se o Login e Senha passados existem no BD
        if Login == Log and Senha == Sen:
            user = self.G.PesquisarUsuario('Id_Acesso',Dados[0][0])
            

            #Pesquisa e Cria um Objeto do tipo Endereco
            End = self.G.PesquisarEndereco('Id',user[0][4])
            print End
            Logradouro = End[0][1]
            Numero = End[0][2]
            Complemento = End[0][3]
            Bairro = End[0][4]
            Cidade = End[0][5]
            Estado = End[0][6]
            Pais = End[0][8]
            CEP = End[0][7]
            endereco = Endereco(Logradouro, Numero, Complemento, Bairro, Cidade, Estado, Pais, CEP)
            
            #Pesquisa e Cria um objeto do tipo Contato
            Con = self.G.PesquisarContato('Id',user[0][5])
            
            Telefone1 = Con[0][1]
            Telefone2 = Con[0][2]
            Telefone3 = Con[0][3]
            Email = Con[0][4]
            contato = Contato(Telefone1, Telefone2, Telefone3, Email)
            
            #Cria um Objeto tipo Usuario
            Nome = user[0][1]
            Sobrenome = user[0][2]
            DataNasc = user[0][3]
            usuario = Usuario(Nome, Sobrenome, DataNasc, Login, Senha, contato, endereco)
            usuario.SetID(user[0][0])
            
            
            self.tela = Frame_PaginaUsuario.create(None, usuario)
            self.tela.Show()
            self.Close()
            
        else:
            #Exibir Mensagem de Erro
            m = "Login e/ou Senha invalidos!"
            t = "Cadastro de Usuario"
            wx.MessageBox(m,t,wx.ICON_ERROR)
            #Limpa os campos de Login e Senha
            self.textCtrl1.SetValue('')
            self.textCtrl2.SetValue('')
            self.textCtrl1.SetFocus()
            


    def OnCadastrarButton(self, event):
        self.tela = Frame_CadastroUsuario.create(None)
        self.tela.Show()

    #Botao: "Ir"
    def OnButton1Button(self, event):
        self.tela = Frame_TelaApresentacao.create(None, None)
        self.tela.Show()

